/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.office_on_the.ducs.services;
import javax.persistence.*;
import java.util.List;
import persistentClass.Mem;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
 * Member Services
 * @author Duc
 */
@WebService
public class Member {
    @WebMethod
    public Boolean persistMem(Mem member){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("Cinema1PU");
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            // do some work
            if(!memExists(member.getMemberCode())){
                tx.begin();
                entityManager.persist(member);
                tx.commit();
                return true;
            }
            else return false;
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }
    @WebMethod
    public List getAllMem(){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("Cinema1PU");
        //Map<String,String> properties = new Map<String,String>();
        //Persistence.createEntityManagerFactory("Cinema1PU", properties)
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
            Query query = entityManager.createNativeQuery("select * from mem",Mem.class);
            List results= query.getResultList();

            tx.commit();
            return results;
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }
    @WebMethod
    public Mem getMem(int memberCode){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("Cinema1PU");
        //Map<String,String> properties = new Map<String,String>();
        //Persistence.createEntityManagerFactory("Cinema1PU", properties)
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
            Query query = entityManager.createNativeQuery("select * from mem where memberCode=?",Mem.class).setParameter(1, memberCode);
            List results= query.getResultList();
            tx.commit();
            if(!results.isEmpty())
                return (Mem) results.get(0);
            else return null;
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }
    @WebMethod
    public Boolean memExists(int memberCode){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("Cinema1PU");
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
            Query query = entityManager.createQuery("from Mem where membercode = :memberCode").setParameter("memberCode", memberCode);
            List result = query.getResultList();
            

            tx.commit();
            return !result.isEmpty();
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }
    @WebMethod
    public void updateMem(Mem member){
       EntityManagerFactory emf= Persistence.createEntityManagerFactory("Cinema1PU");
        //Map<String,String> properties = new Map<String,String>();
        //Persistence.createEntityManagerFactory("Cinema1PU", properties)
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
            entityManager.merge(member);
            tx.commit();
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }
    @WebMethod
    public int deleteMem(int memberCode){
        EntityManagerFactory emf= Persistence.createEntityManagerFactory("Cinema1PU");
        // Non-managed environment idiom
        EntityManager entityManager = emf.createEntityManager();
        EntityTransaction tx = null;
        try {
            tx = entityManager.getTransaction();
            tx.begin();

            // do some work
          	Query query=entityManager.createNativeQuery("delete from mem where membercode=?").setParameter(1, memberCode);

            tx.commit();
        	return query.executeUpdate();//tra lai so luong da xoa
 
        }
        catch (RuntimeException e) {
            if ( tx != null && tx.isActive() ) tx.rollback();
            throw e; // or display error message
        }
        finally {
            entityManager.close();
        }
    }


}
